//
//  CellDetailsViewController.m
//  DeepBreathing
//
//  Created by rimi on 15/12/21.
//  Copyright © 2015年 rimi. All rights reserved.
//

#import "CellDetailsViewController.h"

#import "HeaderView.h"

#import "NetworkManger+NetWorkKingManger.h"

#import "UserModel.h"


#import "RightViewModel.h"







@interface CellDetailsViewController ()

@property(nonatomic,strong)UIWebView *webview;
@property(nonatomic,strong)NSString *string;
@property(nonatomic,strong)RightViewModel *rightModel;

@property(nonatomic,strong)NSDictionary *dic;

@property(nonatomic,strong)UIButton *button;




@end

@implementation CellDetailsViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    
     self.view.backgroundColor  = [UIColor whiteColor];
    
    _webview = [[UIWebView alloc]initWithFrame:CGRectMake(0, 60, CGRectGetMaxX(self.view.frame),CGRectGetMaxY(self.view.frame))];
    
    _webview.backgroundColor = [UIColor whiteColor];
  
    
    [self.view addSubview:_webview];
    
    
    
    
    
    HeaderView *HDV = [[HeaderView alloc]initWithTitleString:@"详情" leftImage:@"返回" rightImage:nil color:[UIColor whiteColor]];
    
    [self.view addSubview:HDV];
    
    _button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    _button.frame = CGRectMake(CGRectGetMaxX(self.view.frame) - 75, 10, 70, 75);
    
    [_button addTarget:self action:@selector(collectionButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_button];

  
    
    [NetworkManger getKnowledgeDetailsWithId:_IDstring successHander:^(id responseObject) {
     
            [_webview loadHTMLString:responseObject[@"data"][@"content"] baseURL:nil];
            if ([responseObject[@"data"][@"favState"] isEqualToString:@"1"]) {
                
                [_button setImage:[UIImage imageNamed:@"心实底"] forState:UIControlStateNormal];
            
            }else if ([responseObject[@"data"][@"favState"] isEqualToString:@"0"]){
                
                [_button setImage:[UIImage imageNamed:@"哮喘收藏_导航"] forState:UIControlStateNormal];
                
            }
            
        
    } failureHander:^(NSError *error) {
        
        [self otherpopuppromptBox:@"请求网络失败"];
        
    }];

    
 
    
    
    HDV.LeftDWblock = ^(){
        
        [self.navigationController popViewControllerAnimated:YES];
         };
    


    
 }






-(void)otherpopuppromptBox:(NSString *)string{
    
    UIAlertController  *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:string preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
    
    
}

-(void)collectionButton:(UIButton *)sender{
    [NetworkManger getKnowledgeDetailsWithId:_IDstring successHander:^(id responseObject) {
        if ([responseObject[@"data"][@"favState"]isEqualToString:@"1"]) {
            [NetworkManger removeFromCollectedWithId:_IDstring successHander:^(id responseObject) {
                [self otherpopuppromptBox:responseObject[@"msg"]];
                [_button setImage:[UIImage imageNamed:@"哮喘收藏_导航"] forState:UIControlStateNormal];
            } failureHander:^(NSError *error) {
                [self otherpopuppromptBox:@"删除失败"];
            }];
        }else{
            [NetworkManger addTocollectWithId:_IDstring successHander:^(id responseObject) {
                [self otherpopuppromptBox:responseObject[@"msg"]];
                [_button setImage:[UIImage imageNamed:@"心实底"] forState:UIControlStateNormal];
            } failureHander:^(NSError *error) {
                [self otherpopuppromptBox:@"添加失败"];
            }];
        }
    } failureHander:^(NSError *error) {
        
    }];
    
    
}


@end
